package com.oracle.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oracle.compents.PageResult;
import com.oracle.mapper.PhCommentMapper;
import com.oracle.pojo.PhComment;
import com.oracle.pojo.PhCommentExample;
import com.oracle.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class CommentServiceImpl implements CommentService {

	@Autowired
	private PhCommentMapper commentMapper;

	/**
	 * 查询全部
	 */
	@Override
	public List<PhComment> findAll() {
		return commentMapper.selecPhyExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		Page<PhComment> page=   (Page<PhComment>) commentMapper.selecPhyExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(PhComment comment) {
		commentMapper.insert(comment);
	}


	/**
	 * 修改
	 */
	@Override
	public void update(PhComment comment){
		commentMapper.updateByPrimaryKey(comment);
	}

	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public PhComment findOne(Integer id){
		return commentMapper.selecPhyPrimaryKey(id);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Integer[] ids) {
		for(Integer id:ids){
			commentMapper.deleteByPrimaryKey(id);
		}
	}


		@Override
	public PageResult findPage(PhComment comment, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);

		PhCommentExample example=new PhCommentExample();
		PhCommentExample.Criteria criteria = example.createCriteria();

		if(comment!=null){
						if(comment.getContent()!=null && comment.getContent().length()>0){
				criteria.andContentLike("%"+comment.getContent()+"%");
			}			if(comment.getUname()!=null && comment.getUname().length()>0){
				criteria.andUnameLike("%"+comment.getUname()+"%");
			}			if(comment.getCreatetime()!=null && comment.getCreatetime().length()>0){
				criteria.andCreatetimeLike("%"+comment.getCreatetime()+"%");
			}
		}

		Page<PhComment> page= (Page<PhComment>)commentMapper.selecPhyExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}

}
